﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Brain.Windows;
using System.IO;

namespace Brain.Editor
{
    public partial class WelcomeForm : Form
    {
        public MainEditor Editor;

        public WelcomeForm(MainEditor editor)
        {
            this.Editor = editor;
            InitializeComponent();
        }

        private void button_New_Click(object sender, EventArgs e)
        {
            NewLevelForm form = new NewLevelForm(this.Editor);
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void button_Load_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Brain Scene Files|*.brain";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (Engine.Instance != null)
                {
                    Engine.EndAndDispose();
                }

                if (GraphicsDeviceService.SingleInstance == null)
                {
                    GraphicsDeviceService.AddRef(this.Handle, 640, 480);
                }
                Engine.InitializeEngineWithDefaultManagers(GraphicsDeviceService.SingleInstance);
                Engine.Instance.Editor = this.Editor;

                using (FileStream str = new FileStream(openFileDialog.FileName, FileMode.Open))
                {
                    Engine.Instance.SerializationManager.Deserialize(str, FileType.XML);
                }

                string dataBaseFile = Path.Combine(Path.Combine(Engine.Instance.SerializationManager.LevelData.RootFolder, "Database.data"));
                if (File.Exists(dataBaseFile))
                {
                    using (FileStream stream = new FileStream(dataBaseFile, FileMode.Open))
                    {
                        DatabaseManager manager =
                            (DatabaseManager)Engine.Instance.SerializationManager.DeserializeUnique(stream, FileType.XML);
                        Engine.Instance.EditorManager.DatabaseManager = manager;
                    }
                }

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void button_Tools_Click(object sender, EventArgs e)
        {
            //BinaryFileReader form = new BinaryFileReader();
            //form.Show();
            HLSLBuilder builder = new HLSLBuilder();
            builder.Show();
        }

        private void button_NewGame_Click(object sender, EventArgs e)
        {
            GameFolderManager folder = new GameFolderManager();
            folder.ShowDialog();
        }

        private void button_ManageGame_Click(object sender, EventArgs e)
        {

        }
    }
}
